package cn.com.w.redis.use.logic.impl;

import cn.com.w.redis.use.domain.UserInfo;
import cn.com.w.redis.use.logic.EXPRankLogic;
import cn.com.w.redis.use.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 经验值(EXP)排行 逻辑实现。
 */
@Component
@Slf4j
public class EXPRankLogicImpl implements EXPRankLogic {


    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    private RedisTemplate redisTemplate;


    private static List<UserInfo> userInfos = new ArrayList<>();

    /**
     * 获取用户列表
     *
     * @return
     */
    private List<UserInfo> listUserInfos(){
        if (CollectionUtils.isEmpty(userInfos)) {
            userInfos = iUserInfoService.list();
        }
        return userInfos;
    }

    @PostConstruct
    public void initUserInfoList(){
        //log.info("开始初始化用户信息");
        //listUserInfos();
    }

    /**
     * 初始化缓存
     */
    @Override
    public void initCache(){
        long start = System.currentTimeMillis();
        List<UserInfo> userInfos = listUserInfos();
        if (CollectionUtils.isEmpty(userInfos)) {
            return;
        }
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        for (UserInfo userInfo : userInfos) {
            double expValue = (double) new Random().nextInt(100000);
            // 想集合中 插入元素， 并设置经验值。
            zSetOperations.add(getRedisKey(), userInfo.getUserName(), expValue);
        }
        log.info("initCache()用时{}毫秒", System.currentTimeMillis() - start);
    }


    @Override
    public void initCacheBatch(){
        long start = System.currentTimeMillis();
        List<UserInfo> userInfos = listUserInfos();
        if (CollectionUtils.isEmpty(userInfos)) {
            return;
        }
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        // 输入组
        Set<DefaultTypedTuple<String>> typedTuples = new HashSet<>();
        for (UserInfo userInfo : userInfos) {
            int expValue = new Random().nextInt(100000);
            DefaultTypedTuple<String> typedTuple = new DefaultTypedTuple<String>(userInfo.getUserName(), (double) expValue);
            typedTuples.add(typedTuple);
        }
        log.info("initCacheBatch()用时{}毫秒", System.currentTimeMillis() - start);
        zSetOperations.add(getRedisKey(), typedTuples);
    }

    @Override
    public void incrementCache() {
        long start = System.currentTimeMillis();
        List<UserInfo> userInfos = listUserInfos();
        if (CollectionUtils.isEmpty(userInfos)) {
            return;
        }
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();

        for (UserInfo userInfo : userInfos) {
            int expValue = new Random().nextInt(100000);
            zSetOperations.incrementScore(getRedisKey(), userInfo.getUserName(), expValue);
        }
        log.info("incrementCache()用时{}毫秒", System.currentTimeMillis() - start);

    }

    /**
     * 获取redis key
     *
     * @return
     */
    @Override
    public String getRedisKey(){
        return "exp:rank";
    }

}
